package com.gton.person.commons.hander;

import com.gton.person.commons.captcha.CaptchaRepository;
import org.springframework.security.authentication.AuthenticationDetailsSource;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: PersonalDesign
 * @description: 验证码的获取
 * @author: GuoTong
 * @create: 2020-11-30 15:13
 **/
public class CaptchaAuthenticationDetailsSource implements AuthenticationDetailsSource<HttpServletRequest, CaptchaAuthenticationDetails> {

    /*根据提交的参数构建CaptchaAuthenticationDetails，
    用户提交的验证码(committedCaptcha)从request中获取，
    预设的验证码(presetCaptcha)从验证码仓库(CaptchaRepostory)获取*/

    private final CaptchaRepository<HttpServletRequest> captchaRepository;

    public CaptchaAuthenticationDetailsSource(CaptchaRepository<HttpServletRequest> captchaRepository) {
        this.captchaRepository = captchaRepository;
    }

    @Override
    public CaptchaAuthenticationDetails buildDetails(HttpServletRequest httpServletRequest) {
        CaptchaAuthenticationDetails captchaAuthenticationDetails = new CaptchaAuthenticationDetails(httpServletRequest);
        captchaAuthenticationDetails.setPresetCaptcha(captchaRepository.load(httpServletRequest));
        return captchaAuthenticationDetails;
    }
}